<?php

namespace app\index\controller;

use app\common\controller\Frontend;

class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        return $this->view->fetch();
    }

    public function test()
    {
        $config = get_addon_config('wanlshop');
        $appid = $config['mp_weixin']['appid'];
        $secret = $config['mp_weixin']['appsecret'];


        $id = $this->request->request("id",0);
        // 加载商品模型
        $goodsModel = model('app\api\model\wanlshop\Goods');
        // 查询商品
        $goods = $goodsModel
            ->where(['id' => $id])
            ->field('id,category_id,shop_category_id,brand_id,freight_id,shop_id,title,image,images,flag,content,category_attribute,activity_type,price,sales,payment,comment,praise,moderate,negative,like,views,status')
            ->find();
        if ($goods){
            // 查询类目
            $goods->category->visible(['id','pid','name']);
            // 查询优惠券
            $goods['coupon'] = [];
            // 查询是否关注
            $goods['follow'] = 0;
            // 查询品牌
            $goods->brand->visible(['name']);
            // 查询SKU
            $goods['sku'] = $goods->sku;
            // 查询SPU
            $goods['spu'] = $goods->spu;
            // 查询直播状态
            $goods['isLive'] = model('app\api\model\wanlshop\Live')->where(['shop_id' => $goods['shop_id'], 'state' => 1])->field('id')->find();
            // 查询评论
            $goods['comment_list'] = $goods->comment_list;
            // 获取店铺详情
            $goods->shop->visible(['shopname','service_ids','avatar','city','like','score_describe','score_service','score_logistics']);
            // 查询快递 运费ID 商品重量 邮递城市 商品数量
            $goods['freight'] = [];
            // 查询促销
            $goods['promotion'] = $id; //--下个版本更新--
            // 店铺推荐
            $goods['shop_recommend'] = $goodsModel
                ->where(['shop_id' => $goods['shop_id'], 'status' => 'normal']) //还可以使用 , 'flag' => 'recommend'
                ->field('id,title,image,price')
                ->limit(3)
                ->select();
            if (!$goods->sku){
                $goods['market_price'] = $goods['price'];
                $goods['interval_price'] = $goods['price'];
            }else{
                $goods['market_price'] = max(array_column($goods->sku,'market_price'));
                $min = min(array_column($goods->sku,'price'));
                $max = max(array_column($goods->sku,'price'));
                $goods['interval_price'] = $min==$max?$min:$min.'-'.$max;
            }
        }

        $this->assign('goods',$goods);

        return $this->view->fetch();
    }

}
